package com.ruoyi.projects.apis;

import cn.hutool.core.lang.Dict;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.IgnoreAuth;
import com.ruoyi.common.annotation.LoginUserInfo;
import com.ruoyi.common.constant.BuConstants;
import com.ruoyi.common.core.controller.ApiBaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.projects.Vos.SlProductVo;
import com.ruoyi.projects.Vos.WxUserInfoDTO;
import com.ruoyi.projects.domain.SlIntegralPro;
import com.ruoyi.projects.domain.SlProduct;
import com.ruoyi.projects.dto.ProductTypeDict;
import com.ruoyi.projects.dto.SlIntegralProVo;
import com.ruoyi.projects.dto.StepsVo;
import com.ruoyi.projects.service.ISlIntegralProService;
import com.ruoyi.projects.service.ISlProductService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店铺产品相关接口
 */
@RestController
@RequestMapping("/api/products")
public class ProductApis extends ApiBaseController {

    private static final Logger log = LoggerFactory.getLogger(ProductApis.class);

    @Autowired
    private ISlProductService slProductService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISlIntegralProService iSlIntegralProService;


    /**
     * 产品分类
     */
    @PostMapping("/category")
    @IgnoreAuth
    public ApiResult category()
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType("product_type");
        if (StringUtils.isNull(data))
        {
            return ApiResult.success(new ArrayList<ProductTypeDict>());
        }
        ArrayList<ProductTypeDict> productTypeDicts = new ArrayList<>();
        productTypeDicts.add(new ProductTypeDict(0L,"全部","0","product_type"));
        data.forEach(item ->{
            ProductTypeDict productTypeDict = new ProductTypeDict();
            BeanUtils.copyProperties(item,productTypeDict);
            productTypeDicts.add(productTypeDict);
        });
        return ApiResult.success(productTypeDicts);
    }

    /**
     * 产品操作步骤查询
     */
    @PostMapping("/operateLists")
    @IgnoreAuth
    public ApiResult operateLists()
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType("operate_list");
        if (StringUtils.isNull(data))
        {
            return ApiResult.success(new ArrayList<StepsVo>());
        }
        ArrayList<StepsVo> stepsVoArrayList = new ArrayList<>();
        data.forEach(item ->{
            StepsVo stepsVo = new StepsVo();
            stepsVo.setDesc(item.getDictValue());
            stepsVo.setTitle(item.getDictLabel());
            stepsVoArrayList.add(stepsVo);
        });
        return ApiResult.success(stepsVoArrayList);
    }

    /**
     * 注意事项字典
     */
    @PostMapping("/noticeLists")
    @IgnoreAuth
    public ApiResult noticeLists()
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType("product_notices");
        if (StringUtils.isNull(data))
        {
            return ApiResult.success(new ArrayList<ProductTypeDict>());
        }
        ArrayList<String> strings = new ArrayList<>();
        data.forEach(item ->{
            strings.add(item.getDictValue());
        });
        return ApiResult.success(strings);
    }

    /**
     * 获取产品列表
     */
    @PostMapping("/getList")
    @IgnoreAuth
    public TableDataInfo getList(SlProductVo vo)
    {
        Integer pageNum = vo.getPageNum();
        Integer pageSize = vo.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            PageHelper.startPage(pageNum, pageSize);
        }else {
            startPage();
        }
        if (StringUtils.isNotNull(vo.getCategoryId())&& BuConstants.PRODUCT_CATEGORY_ALL.equals(vo.getCategoryId())){
            vo.setCategoryId(null);
        }
        List<SlProductVo> slProducts = slProductService.queryAllProducts(vo);
        return getDataTable(slProducts);
    }

    /**
     * 产品详情
     */
    @PostMapping("/detail")
    @IgnoreAuth
    public ApiResult detail(@RequestBody SlProductVo vo)
    {
        if (StringUtils.isNotNull(vo.getId())){
            SlProduct slProduct = slProductService.selectSlProductById(vo.getId());
            return ApiResult.success(slProduct);
        }else {
            return ApiResult.success(new SlProduct());
        }
    }


    /**
     * 兑换积分产品页面初始化
     */
    @PostMapping("/integralShopInit")
    public ApiResult integralShopInit(@LoginUserInfo WxUserInfoDTO dto, SlIntegralProVo vo)
    {
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("integral",dto.getIntegral());
        Integer pageNum = vo.getPageNum();
        Integer pageSize = vo.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            PageHelper.startPage(pageNum, pageSize);
        }else {
            startPage();
        }
        List<SlIntegralPro> slIntegralPros = iSlIntegralProService.selectSlIntegralProList(new SlIntegralPro());
        if (slIntegralPros.size()>0){
            resultMap.put("integralList",slIntegralPros);
        }else {
            resultMap.put("integralList",new ArrayList<SlIntegralPro>());
        }
        return ApiResult.success(resultMap);
    }



}






















